package 面试;

import java.util.concurrent.*;

public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,   //核心线程数
                5, //最大线程数
                1l,    //空闲线程最大等待时间
                TimeUnit.SECONDS,  // keepAliveTime单位
                new LinkedBlockingDeque<Runnable>(3), //等待任务队列
                Executors.defaultThreadFactory(), //线程工程
                new ThreadPoolExecutor.DiscardPolicy() ); //拒绝策略
        try{
            //模拟10个人去只有5个窗口的银行办理业务
            for (int i = 0; i < 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }

    }

    public static void main01(String[] args) {
        //查看电脑的核数
        //System.out.println(Runtime.getRuntime().availableProcessors());
        //
        // 接口 ：接口辅助工具类
        //Array: Arrays
        //Collection: Collections
        //Executor: Executors


        //ExecutorService threadPool = Executors.newFixedThreadPool(5);//一池5线程
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();//一池1线程
        ExecutorService threadPool = Executors.newCachedThreadPool();//一池N线程

        try{
            //模拟10个人去只有5个窗口的银行办理业务
            for (int i = 0; i < 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
         }finally {
            threadPool.shutdown();
        }
    }
}
